import Fly from 'flyio/dist/npm/wx';
import configs from './configs';

const request = new Fly();

request.config.baseURL = configs.baseUrl;

request.interceptors.request.use((request) => {
  mpvue.showNavigationBarLoading();
  request.headers = {
    'Content-Type': 'application/json',
    'token': mpvue.getStorageSync('token')
  }
  return request;
});
request.interceptors.response.use(
  (response, promise) => {
    mpvue.hideNavigationBarLoading();
    if (response.data.code == 0) {
      return promise.resolve(response.data);
    } else if (response.data.code == 401) {
      // 授权已过期
      mpvue.removeStorageSync('token');
      mpvue.reLaunch({
        url: '/pages/auth/index'
      });
      return promise.reject(response.data);
    } else {
      return promise.reject(response.data);
    }
  },
  (err, promise) => {
    mpvue.hideNavigationBarLoading();
    mpvue.showToast({
      title: err.status+'',
      icon: 'none'
    });
    return promise.reject()
  }
)
export default request;
